package com.sh.链表;

import java.util.HashMap;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class 复制带随机指针的链表 {
    public static void main(String[] args) {

    }

    public Node copyRandomList(Node head) {
        HashMap<Node, Node> resMap = new HashMap<>();
        Node temp = head;
        while (temp!=null){
            resMap.put(temp, new Node(temp.val));
            temp = temp.next;
        }
        temp = head;
        while (temp!=null){
            Node node = resMap.get(temp);
            node.next = resMap.get(temp.next);
            node.random = resMap.get(temp.random);
            temp = temp.next;
        }
        return resMap.get(head);
    }
}

